<template>
  <!-- userSet 用户设置 -->
  <div style="margin-top: 15px;">
    <!-- # 表格 -->
    <el-table v-loading="loading" element-loading-text="正在加载中" :data="tableData" style="width: 99%">
      <el-table-column label="序号" width="80" align="center" type="index" />

      <el-table-column label="用户名" width="150" align="center">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>用户名: {{ scope.row.username }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="昵称" width="100" prop="nickname" />

      <el-table-column label="性别" width="140" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == '0'">女</span>
          <span v-if="scope.row.sex == '1'">男</span>
        </template>
      </el-table-column>
      <el-table-column label="角色" width="140" align="center">
        <template slot-scope="scope">
          <!-- TODO: 角色 -->
          {{ scope.row.userType === '0' ? '管理员' : '普通用户' }}
        </template>
      </el-table-column>
      <el-table-column label="用户状态" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="success">正常</el-tag>
          <el-tag v-if="scope.row.status == 1" type="info">停用</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="手机号" width="150" prop="phone" />

      <el-table-column label="邮箱" width="180" prop="email" />

      <el-table-column label="创建时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" width="250">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ formatDate(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="250" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" plain @click="handleSet(scope.row)">分配角色</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button v-if="scope.row.status == 0" size="mini" type="danger" @click="handleDelete(scope.row)">停用</el-button>
          <el-button v-if="scope.row.status == 1" size="mini" disabled type="danger" @click="handleDelete(scope.row)">停用</el-button>
        </template>
      </el-table-column>
      <template v-slot:empty>
        <div style="height: 200px;">
          <img src="../../assets/nodata.png" width="150" style="margin: 30px;">
        </div>
      </template>
    </el-table>
    <!-- # 添加/编辑弹层 -->
    <el-dialog :title="isEdit ? '编辑' : '添加'" :visible.sync="dialogVisible" width="50%">
      <div>
        <el-form ref="formDateRef" :rules="rules" label-position="right" label-width="80px" :model="formDate">
          <el-form-item label="姓名" prop="username">
            <el-input v-model="formDate.username" />
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="formDate.nickname" />
          </el-form-item>

          <el-form-item label="性别" prop="sex">
            <el-select v-model="formDate.sex" placeholder="请选择性别" style="width: 100%">
              <el-option v-for="option in sexList" :key="option.value" :label="option.label" :value="option.value" />
            </el-select>
          </el-form-item>

          <el-form-item label="手机号" prop="phone">
            <el-input v-model="formDate.phone" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="formDate.email" />
          </el-form-item>

        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelBtn">取 消</el-button>
        <el-button type="primary" @click="verBtn">确 定</el-button>
      </span>
    </el-dialog>

    <!-- # 分配角色弹层 -->
    <el-dialog title="分配角色" :visible.sync="setDialogVisible" width="30%">
      <el-form label-position="right" label-width="100px" :model="formDate">
        <el-form-item label="请选择角色" prop="sex">
          <el-select v-model="formDate.userType" placeholder="请选择角色" style="width: 100%">
            <el-option v-for="option in userType" :key="option.value" :label="option.label" :value="option.value" />
          </el-select>
        </el-form-item>
      </el-form>
      <!-- <el-select v-model="value" placeholder="请选择角色" style="width: 100%">
        <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value" />
      </el-select> -->

      <span slot="footer" class="dialog-footer">
        <el-button @click="setCancelBtn">取 消</el-button>
        <el-button type="primary" @click="SetVerBtn">确 定</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
import { getUserInfo, disableUser, updateUser } from '@/api/userinfo.js'
import { getRoleAll } from '@/api/role.js'
import { formatDate } from '@/utils/index.js'
export default {
  name: 'OperationDuty',
  data() {
    return {
      tableData: [], // 全部用户信息
      roleData: [], // 所有角色
      options: [], // 分配角色列表
      region: [],
      // 编辑数据
      formDate: {
        username: '',
        nickname: '',
        userType: '',
        roles: '',
        email: '',
        phone: '',
        sex: ''

      },
      rules: {
        username: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        sex: [{ required: true, message: '请选择性别', trigger: 'blur' }],
        phone: [{ required: true, message: '请填写手机号', trigger: 'blur' }],
        email: [{ required: true, type: 'email', message: '请填写正确邮箱', trigger: 'blur' }]

      },
      value: '',
      setVerID: '',
      dialogVisible: false, // 弹层是否开启
      setDialogVisible: false,
      isEdit: true, // 是否是编辑
      currentPage3: 1, // 当前页数
      loading: true,
      sexList: [
        {
          value: '0',
          label: '女'
        }, {
          value: '1',
          label: '男'
        }
      ],
      userType: [
        {
          value: '0',
          label: '管理员'
        }, {
          value: '1',
          label: '普通角色'
        }
      ]
    }
  },
  mounted() {
    this.getUserInfo()
  },
  methods: {
    formatDate,
    /**
     * 用户分配角色
     */
    // 获取所有角色
    async getRoleAll() {
      const res = await getRoleAll()
      this.rolesData = res
      console.log('所有角色', this.rolesData)
    },
    // 分配角色按钮
    async handleSet(row) {
      this.setDialogVisible = true
      this.formDate = {
        id: row.id,
        username: row.username,
        nickname: row.nickname,
        sex: row.sex,
        roles: row.roles,
        email: row.email,
        phone: row.phone,
        userType: row.userType
      }
    },
    setCancelBtn() {
      this.setDialogVisible = false
      this.$message.info('取消分配')
    },
    async SetVerBtn() {
      this.setDialogVisible = false
      console.log('分配角色', this.formDate)
      // TODO 分配角色
      await updateUser(this.formDate)
      await this.getUserInfo()
      this.$message({
        message: '分配成功！',
        type: 'success'
      })
    },

    formatPassword(password) {
      // 这里简单地用相同数量的星号替换密码字符，也可以根据需要进行自定义
      return '*'.repeat(password.length)
    },
    // 获取表格数据
    async getUserInfo() {
      const res = await getUserInfo()
      this.tableData = res
      console.log('全部用户', this.tableData)
      this.loading = false
    },

    /**
     * 编辑
     */
    handleEdit(row) {
      console.log('id', row.id)
      this.dialogVisible = true
      this.isEdit = true
      this.formDate = {
        id: row.id,
        username: row.username,
        nickname: row.nickname,
        sex: row.sex,
        roles: row.roles,
        email: row.email,
        phone: row.phone
      }
    },
    // 停用
    handleDelete(row) {
      console.log('id', row.id)
      this.$confirm('此操作将停用该账户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await disableUser(row.id)
        this.$message({
          type: 'success',
          message: '停用成功!'
        })
      })
    },
    // 弹层 取消
    cancelBtn() {
      this.dialogVisible = false
      this.$message({
        message: '已取消！',
        type: 'info'
      })
    },
    // 弹层 确认
    async verBtn() {
      this.$refs.formDateRef.validate(async isok => {
        if (isok) {
          await updateUser(this.formDate)
          this.$message({
            message: '编辑成功！',
            type: 'success'
          })
          this.getUserInfo()
          this.dialogVisible = false
        }
      })

      // 编辑-确认
      if (this.isEdit) {
        console.log(this.formDate)
        // 添加-确认
      } else {
        // console.log(this.formDate)
        // 添加接口
        // await addPatrol(this.formDate)
        this.$message({
          message: '添加成功！',
          type: 'success'
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
